package com.moogene.android.engine.animlite;

import android.graphics.Rect;
import android.util.Log;

public class ScaleAnim extends Anime {
	private String TAG = "ScaleAnim";

	private double mCurScale = 1;
	private double mToScale;
	private double mStepScale;
	private boolean mIsToInt;
    private Rect dstRect;

	ScaleAnim(DrawableWrapper drawableWrapper) {
        super(drawableWrapper);
        dstRect = new Rect();
    }

	public void start(int toScale, double stepScale) {
		start(mCurScale, toScale, stepScale);
		mIsToInt = true;
	}
	
	public void start(int from, int toScale, double stepScale) {
		start((double)from, (double)toScale, stepScale);
		mIsToInt = true;
	}

	public void start(double toScale, double stepScale) {
		start(mCurScale, toScale, stepScale);
	}

	public void start(double fromScale, double toScale, double stepScale) {
		Log.v(TAG, "Scale Anim; from: " + fromScale + "; to: " + toScale
				+ "; step: " + stepScale);
		if (toScale < 0) {
			Log.d(TAG, "Animation.startScale(); toScale is illegal");
			throw new IllegalArgumentException();
		}
		if ((toScale >= fromScale && stepScale < 0)
				|| (toScale <= fromScale && stepScale > 0)) {
			Log.d(TAG, "startScale(); param is illegal; toScale=" + toScale
					+ "; fromScale=" + fromScale + "; stepScale=" + stepScale);
			throw new IllegalArgumentException();
		}
		Log.v(TAG, "Scale Anim arguments check pass");
		start();
		mCurScale = fromScale;
		mStepScale = stepScale;
		mToScale = toScale;
		mIsToInt = false;
	}
	
	public double getCurrent(){
		return mCurScale;
	}

	Rect getDstRect(int left, int top, int width, int height, int anchorX, int anchorY) {
		if(mCurScale == 0){
			Log.d(TAG, "mCurScale = 0, nothing to draw");
			return null;
		}
		int aX = anchorX;
		int aY = anchorY;
		switch (mState) {
		case STATE_RUNNING:
			dstRect.left = (int) (left - aX * mCurScale);
			dstRect.top = (int) (top - aY * mCurScale);
			dstRect.right = (int) (left + (width - aX) * mCurScale);
			dstRect.bottom = (int) (top + (height - aY) * mCurScale);
			break;
		case STATE_PAUSE:
			if (mIsToInt) {
				dstRect.left = left - aX * (int) Math.round(mCurScale);
				dstRect.top = top - aY * (int) Math.round(mCurScale);
				dstRect.right = left + (width - aX)
						* (int) Math.round(mCurScale);
				dstRect.bottom = top + (height - aY)
						* (int) Math.round(mCurScale);
			} else {
				dstRect.left = (int) (left - aX * mCurScale);
				dstRect.top = (int) (top - aY * mCurScale);
				dstRect.right = (int) (left + (width - aX) * mCurScale);
				dstRect.bottom = (int) (top + (height - aY) * mCurScale);
//				dest = new Rect(
//						(int) (left - aX * mCurScale),
//						(int) (top - aY * mCurScale),
//						(int) (left + (width - aX) * mCurScale),
//						(int) (top + (height - aY) * mCurScale));
			}
			break;
		case STATE_STOP:
			dstRect.left = left - aX;
			dstRect.top = top - aY;
			dstRect.right = left + width - aX;
			dstRect.bottom = top + height - aY;
//			dest = new Rect(left - aX,
//			        top - aY,
//			        left + width - aX,
//			        top + height - aY);
			break;
		}
		return dstRect;
	}

	@Override
	boolean nextFrame() {
		if (!super.nextFrame()) {
			return false;
		}
		mCurScale += mStepScale;
		System.out.println("mCurScale: " + mCurScale);
		if ((mStepScale > 0 && mCurScale >= mToScale)
				|| (mStepScale < 0 && mCurScale <= mToScale)) {
			pause();
			return false;
		}
		return true;
	}
}
